<?php
set_time_limit(0);

require_once(dirname(__FILE__) . '/../config/saasapi.php');

define('SASS_APP_KEY', 'taoguan');
define('SAAS_SECRE_KEY', '49F4589687E79D815339B13A73E5FBB4');

function fetchHostListByCode($code) {

    $api = new SaasOpenClient();
    $api->appkey = SASS_APP_KEY;
    $api->secretKey = SAAS_SECRE_KEY;
    $api->format = 'json';

    $params = array('service_code' => $code);
    $result = $api->execute('host.getlist', $params);

    unset($api);
    if ($result->success == 'true') {
        if ($result->data == 'QUEUE_END') {
            return null;
        } else {
            return $result->data;
        }
    } else {
        return null;
    }
}

function getInfoByHost($host) {

    $api = new SaasOpenClient();
    $api->appkey = SASS_APP_KEY;
    $api->secretKey = SAAS_SECRE_KEY;
    $api->format = 'json';

    $params = array('server_name' => $host);
    $result = $api->execute('host.getinfo_byservername', $params);

    unset($api);
    if ($result->success == 'true') {
        if ($result->data == 'QUEUE_END') {
            return null;
        } else {
            return $result->data;
        }
    } else {
        return null;
    }
}

function callUpdateProcess($host, $date) {
 
    $info = getInfoByHost($host);
    if ($info->status != 'HOST_STATUS_DELETED') {
        
        $serverName = $host;
        $start = preg_replace('/ 00:00:00$/is', '', $info->cycle_start);
	$end = preg_replace('/ 00:00:00$/is', '', $info->cycle_end);
	$hostId = $info->host_id;

	$cmd = sprintf("/usr/local/php/bin/php /data/httpd/dev.fh.taoex.com/script/tools/statistics.php %s %s %s %s %s", $serverName, $hostId,  $start, $end, $date);
	exec($cmd ,$b, $a);
    }
}


//$finfo = fetchHostListByCode('taoex-fcfh');
$tinfo = fetchHostListByCode('ts-12093');
$time = time();

$fileName = realpath(dirname(__FILE__) . '/logs/');
$fileName = $fileName . '/data/' . date('Y-m-d', $time) . '/';
@mkdir($fileName, 0777, true);
$fileName =  $fileName . 'data.php';
$fp = fopen($fileName, 'w');
fwrite($fp, "<?\n\n" . "\n");
fclose($fp);

$info = array_merge((array) $finfo->host_name_list, (array) $tinfo->host_name_list);
foreach ((array) $info as $host) {

    callUpdateProcess($host, $time);
}
